package com.meituan.mapper;

import com.meituan.model.GoodsEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface GoodsMapper {
    @Insert("insert into goods(classify_id,shop_id,goods_name,goods_price,goods_characteristics,register_time) values(#{classifyId},#{shopId},#{goodsName},#{goodsPrice},#{goodsCharacteristics},#{registerTime})")
    Integer save(GoodsEntity goodsEntity);

    @Select("select * from goods where shop_id = #{shopId}")
    @Results({
            @Result(property = "goodsId",column = "goods_id"),
            @Result(property = "monthSales",column = "goods_id",
            one = @One(select="com.meituan.mapper.UtilMapper.findCountByGoodsId",fetchType = FetchType.EAGER))
    })
    List<GoodsEntity> findByShopId(String shopId);

    @Select("select * from goods where classify_id = #{GoodsClassifyId}")
    List<GoodsEntity> findByClassifyId(String GoodsClassifyId);

    @Update("update goods set goods_name = #{goodsName},goods_price=#{goodsPrice},goods_characteristics=#{goodsCharacteristics} where goods_id=#{goodsId}")
    Integer updateGoods(GoodsEntity goodsEntity);

    @Select(("select * from goods where shop_id = #{shopId}"))
    @Results({
            @Result(property = "classifyId",column = "classify_id"),
            @Result(property = "goodsId",column = "goods_id"),
            @Result(property = "monthSales",column = "goods_id",
            one=@One(select = "com.meituan.mapper.UtilMapper.findCountByGoodsId",fetchType = FetchType.EAGER)),
            @Result(property = "goodsClassifyEntity",column = "classify_id",
            one = @One(select = "com.meituan.mapper.GoodsClassifyMapper.findById",fetchType = FetchType.EAGER))
    })
    List<GoodsEntity> findPageByShopIdWithClassify(String shopId);

    @Delete("delete from goods where goods_id = #{goodsId}")
    Integer delGoods(String goodsId);

    @Delete("delete from goods where classify_id = #{classifyId}")
    Integer delGoodsByClassifyId(String classifyId);

    @Update(("update goods set goods_img = #{goodsImg} where goods_id = #{goodsId}"))
    Integer updateImg(@Param("goodsImg") String goodsImg,@Param("goodsId") String goodsId);
}
